﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    [HotUpdate]
    [Description("抓取网址中的关键信息内容")]
    public class CatchHTTP : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("F_QZPV_Remark");
            e.FieldKeys.Add("FMATERIALID");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            foreach (DynamicObject item in e.DataEntitys)
            {
                string MATERIALID = "", F_QZPV_Remark = "";
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    MATERIALID = Convert.ToString(item["ID"]);
                }
                if (item["F_QZPV_Remark"] != null && Convert.ToString(item["F_QZPV_Remark"]) != "")
                {
                    F_QZPV_Remark = Convert.ToString(item["F_QZPV_Remark"]);
                }
                if (F_QZPV_Remark.Contains("spm"))
                {
                    int startIndex = F_QZPV_Remark.IndexOf("offer/") + "offer/".Length;
                    int endIndex = F_QZPV_Remark.IndexOf(".html", startIndex);
                    string productId = "";
                    if (startIndex != -1 && endIndex != -1)
                    {
                         productId = F_QZPV_Remark.Substring(startIndex, endIndex - startIndex);
                    }
                    string SQL = "/*dialect*/" + string.Format(@"UPDATE T SET t.F_WNEG_TEXT18='{0}'   from T_BD_MATERIAL t
                    where FMATERIALID={1}
                   ", productId,MATERIALID);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL);
                }
                if(F_QZPV_Remark.Contains("http://detail.m.1688.com/page/index.htm"))
                {
                    int startIndex = F_QZPV_Remark.IndexOf("offerId=") + "offerId=".Length;
                    string result = F_QZPV_Remark.Substring(startIndex);
                    string SQL = "/*dialect*/" + string.Format(@"UPDATE T SET t.F_WNEG_TEXT18='{0}'   from T_BD_MATERIAL t
                    where FMATERIALID={1}
                   ", result, MATERIALID);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL);
                }
               
            }
        }
    }
}
